//
//  GoogleMapWithBuyViewController.m
//  EasyBuy
//
//  Created by apple on 12-10-24.
//  Copyright (c) 2012年 shen donghao. All rights reserved.
//

#import "GoogleMapWithBuyViewController.h"
#import "GoogleMapAnnotation.h"


@interface GoogleMapWithBuyViewController ()

@end

@implementation GoogleMapWithBuyViewController
@synthesize map;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    locmanager = [[CLLocationManager alloc] init]; 
    [locmanager setDelegate:self]; 
    [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];	
    [locmanager startUpdatingLocation];
    CGRect rect = CGRectMake(0, 0, 320, 480);
    map = [[MKMapView alloc] initWithFrame:rect];
    [map setDelegate: self];
	[map setMapType: MKMapTypeStandard];
    map.showsUserLocation=YES;
    [self.view addSubview:map];
    
    
}

- (void)setBuyLocationInGoogleMapWithlongitude:(double)lon latitude:(double)lat title:(NSString *)title subtitle:(NSString *)subtitle{
    latitude = (float)lat;
    longitude = (float)lon;
    
    CLLocationCoordinate2D Coordinate;
	
	Coordinate.latitude = latitude;
	Coordinate.longitude =longitude;
	GoogleMapAnnotation *annotation = [[GoogleMapAnnotation alloc] initWithCoordinate:Coordinate];
	[annotation setTitle:title];
	[annotation setSubtitle:subtitle];
	[annotation setAnnotationType:iCodeBlogAnnotationTypeApple];
    
	[map addAnnotation:annotation];
    
	CLLocationCoordinate2D theCoordinate;
	CLLocationCoordinate2D theCenter;
	
	theCoordinate.latitude =latitude;
	theCoordinate.longitude=longitude;
    
    
	MKCoordinateRegion theRegin;//查找范围的中心点（圆心）
	theCenter.latitude =latitude;
	theCenter.longitude = longitude;
	theRegin.center=theCenter;
	
	MKCoordinateSpan theSpan;//查找范围的半径
	theSpan.latitudeDelta = 0.1;
	theSpan.longitudeDelta = 0.1;
	theRegin.span = theSpan;
    [map setRegion:theRegin];
    [map regionThatFits:theRegin];
    [annotation release];

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
	//method one：using default pin as a PlaceMarker to display on map
	NSLog(@"creatview");
	MKPinAnnotationView *newAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"]autorelease];
	//newAnnotation.pinColor = MKPinAnnotationColorGreen;
	newAnnotation.animatesDrop = YES; 
	//canShowCallout: to display the callout view by touch the pin
	newAnnotation.canShowCallout=YES;
	return newAnnotation;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc{
    [map release];
    [locmanager release];
    
    [super dealloc];
}
@end
